<script setup>
import pageCom from '@/components/pagecontainer.vue'
import { onMounted, ref } from 'vue'
import { userStore } from '@/stores';
import { userupdateImgservice } from '@/api/user'
const userstore = userStore()
const title = ref('用户头像')
const imageUrl = ref(userstore.user.user_pic)
const upload = ref()
const onSelectFile = (uploadFile) => {
  const reader = new FileReader()
  reader.readAsDataURL(uploadFile.raw)
  reader.onload = () => {
    imageUrl.value = reader.result
  }
}
const onsubmit = async () => {
  await userupdateImgservice(imageUrl.value)
  await userstore.getUser()
  ElMessage.success('上传头像成功')

}
</script>

<template>
  <pageCom :title="title">
    <el-upload ref="upload" class="avatar-uploader" :on-change="onSelectFile" :auto-upload="false"
      :show-file-list="false">
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon">
        <Plus />
      </el-icon>
    </el-upload>
    <div class="footer">
      <el-button type="primary" @click="upload.$el.querySelector('input').click()">
        <el-icon>
          <Plus />
        </el-icon>
        &nbsp;选择头像
      </el-button>
      <el-button type="success" @click="onsubmit"><el-icon>
          <Upload />
        </el-icon>
        &nbsp;上传头像</el-button>
    </div>
  </pageCom>
</template>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.footer {
  margin-top: 20px;
}
</style>